package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    public DishService dishService;

    @Autowired
    public CategoryService categoryService;

    @Autowired
    public DishFlavorService dishFlavorService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {

//        dishService.save(dish);
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");

    }

    @GetMapping("/page")
    public R<Page> getAll(Integer page, Integer pageSize, String name) {
        Page<Dish> page1 = new Page<>(page, pageSize);

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(name), Dish::getName, name);
        wrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(page1, wrapper);


        List<Dish> dishes = page1.getRecords();

        Page<DishDto> page2 = new Page<>(page, pageSize);

        List<DishDto> dishdto = new ArrayList<>();


        for (Dish dish : dishes) {

            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(dish, dishDto);

            Long categoryId = dish.getCategoryId();

            Category categorybyId = categoryService.getById(categoryId);

            dishDto.setCategoryName(categorybyId.getName());

            dishdto.add(dishDto);

        }


        page2.setRecords(dishdto);

        page2.setTotal(page1.getTotal());


        return R.success(page2);

    }


    //修改回显
    @GetMapping("/{id}")
    public R<DishDto> getdto(@PathVariable Long id) {

        DishDto dishDto = dishService.getByIdwithFlavor(id);

        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {

        dishService.updatwithFlavor(dishDto);
        return R.success("aaa");
    }

//    @GetMapping("/list")
//    public R<List<Dish>> dm(Long categoryId){
//        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
//
//        wrapper.eq(Dish::getCategoryId,categoryId).eq(Dish::getStatus,1);
//
//        List<Dish> list = dishService.list(wrapper);
//
//        return R.success(list);
//
//    }


    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null ,Dish::getCategoryId,dish.getCategoryId());
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(Dish::getStatus,1);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(queryWrapper);

        List<DishDto> dishDtoList = list.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);

            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }

            //当前菜品的id
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
            //SQL:select * from dish_flavor where dish_id = ?
            List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);
            dishDto.setFlavors(dishFlavorList);

            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtoList);
    }
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

       dishService.removeWith(ids);
        return R.success("删除成功");

    }

    @PostMapping("/status/{st}")
    public R<String> destatus(@PathVariable int st,@RequestParam List<Long> ids){
        List<Dish> dishes = ids.stream().map((item) -> {
            Dish dish = new Dish();
            dish.setId(item);
            dish.setStatus(st);
            return dish;
        }).collect(Collectors.toList());

        dishService.updateBatchById(dishes);


        return R.success("修改成功");

    }


}
